package com.example.laboratory.root_ui.AddLaboratory;

import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.example.laboratory.R;
import com.example.laboratory.application.LApp;
import com.example.laboratory.bean.Items;
import com.example.laboratory.bean.Laboratory;
import com.example.laboratory.bean.UserList;
import com.example.laboratory.manager.UserInfoManager;
import com.example.laboratory.root_ui.dialog.ProItemListDialog;
import com.example.laboratory.ui.base.BasePresenterActivity;

import java.util.ArrayList;
import java.util.List;

public class AddLaboratoryActivity extends BasePresenterActivity<AddLabPresenter> implements AddLabContact.IAddLabsView {


    @BindView(R.id.lab_label)
    EditText labLabel;
    @BindView(R.id.add_lab_state)
    Spinner labState;
    @BindView(R.id.add_lab_function)
    EditText labFunction;
    @BindView(R.id.add_lab_category)
    Spinner labCategory;
    @BindView(R.id.add_lab_uid)
    Spinner labUid;
    @BindView(R.id.add_lab_depart)
    TextView labDepart;
    @BindView(R.id.add_lab_safe_status)
    Spinner labSafeStatus;
    @BindView(R.id.add_labs)
    Button addLabs;

    List<String> lab_state_data = new ArrayList<>();
    List<String> lab_safe_status_data = new ArrayList<>();

    List<String> lab_category_data = new ArrayList<>();
    List<String> lab_user_data = new ArrayList<>();
    ArrayAdapter<String> spinner_lab_category_adapter;
    ArrayAdapter<String> spinner_lab_state_adapter;
    ArrayAdapter<String> spinner_lab_safe_status_adapter;

    ArrayAdapter<String> spinner_lab_user_adapter;
    String state_check;
    String safe_status_check;
    String category_check;
    @BindView(R.id.item_list_select)
    TextView itemListSelect;
    List<String> itemList=new ArrayList<>();
    List<Items.ItemListBean> itemListBeans =new ArrayList<>();
    List<UserList.UserListBean> userListBeans =new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_laboratory;
    }


    @Override
    protected boolean initToolbar() {
        mToolbar.setTitle("新增实验室");
        return true;
    }
    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mPresenter.getItemsData(UserInfoManager.getUserInfo().getDepartId());
        mPresenter.getUsersData(UserInfoManager.getUserInfo().getDepartId(),"1");



    }
    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        lab_state_data.add("运行");
        lab_state_data.add("建设中");
        lab_safe_status_data.add("安全");
        lab_safe_status_data.add("存在隐患");

        lab_category_data.add("开放机房");
        lab_category_data.add("研究生工作室");
        lab_category_data.add("博士生研究室");



        // labFunction.setText("XXX");
        spinner_lab_state_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lab_state_data);
        spinner_lab_safe_status_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lab_safe_status_data);
        spinner_lab_category_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lab_category_data);
         labCategory.setAdapter(spinner_lab_category_adapter);
        labSafeStatus.setAdapter(spinner_lab_safe_status_adapter);
        labState.setAdapter(spinner_lab_state_adapter);


        labState.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                state_check = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });

        labCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                category_check = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });
        labSafeStatus.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                safe_status_check = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });


    }

    @OnClick(R.id.add_labs)
    public void onAddClicked() {
        Laboratory.RowsBean lab = new Laboratory.RowsBean();
        lab.setLabel(labLabel.getText() + "");
        lab.setState(state_check);
        lab.setFunction(labFunction.getText() + "");
        lab.setCategory(category_check);
        lab.setLabUid(UserInfoManager.getUserInfo().getUid());
        lab.setDepartId(labDepart.getText().toString());
        lab.setSafeStatus(safe_status_check);

    }

    @OnClick(R.id.item_list_select)
    public void onItemsDialogClicked() {
        Log.d("SS",userListBeans.get(0).getDepartId());
        for (int i = 0; i < itemListBeans.size() ; i++) {
            itemList.add(itemListBeans.get(i).getItemname());
        }
        Dialog dialog=new ProItemListDialog(this,itemList);
        dialog.show();

        for (int i = 0; i <userListBeans.size() ; i++) {
            lab_user_data.add(userListBeans.get(i).getUsername()+" "+userListBeans.get(i).getUid());
        }

        spinner_lab_user_adapter=new ArrayAdapter<>(this,android.R.layout.simple_spinner_item, lab_user_data);
        labUid.setAdapter(spinner_lab_user_adapter);
        labUid.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                safe_status_check = parent.getItemAtPosition(position).toString();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub
            }
        });


    }




    @Override
    protected AddLabPresenter createPresenter() {
        return new AddLabPresenter();
    }

    @Override
    public void showResult(String msg) {

    }

    @Override
    public void setItemList(List<Items.ItemListBean> itemList) {
        this.itemListBeans=itemList;
    }

    @Override
    public void setUserList(List<UserList.UserListBean> userList) {
        this.userListBeans=userList;
    }


}
